﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Data;

namespace LGSVR_WEB
{
    public partial class ExtMain : LGSVR.Common.DataBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                PageInit();

            }
        }

        private void PageInit()
        {
            btn_UserInfo.OnClientClick = this.tabs_Main.GetAddTabReference("tab_UserInfo", "sys/UserInfoEdit.aspx?action=edit", this.btn_UserInfo.Text, IconHelper.GetIconUrl(btn_UserInfo.Icon), true);
            //this.btn_ChangePassword.OnClientClick = this.win_ChangePassword.GetShowReference();
            DataTable dt;
            int totalRS;
            //if (Session["actID"] != null)
            //{
            //    //只查询出有权限的菜单
             dt = GetList("menuInfo", " id desc ", " AND menuID IN (SELECT menuID FROM ActMenu where state ='1' AND actID = '" + Session["actID"] + "')", 1, 500, out totalRS);
            //dt = GetList("menuInfo", " id desc ","", 1, 500, out totalRS);
            this.CreatMenu("节目管理", dt, this.tree_program, this.ap_program, "~/icon/tabpanel.png");
            this.CreatMenu("终端管理", dt, this.tree_terminal, this.ap_terminal, "~/icon/tabpanel.png");
            this.CreatMenu("系统管理", dt, this.tree_system, this.ap_system, "~/icon/tabpanel.png");

            this.tbt_Clock.Text ="登录时间："+ DateTime.Now.ToString();

            this.tbt_Info.Text = String.Format("【昵称：{0}】&nbsp;【角色：{1}】", Session["UserName"], Session["ActName"]);

            //}
        }

        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <param name="modalName">模块名称</param>
        /// <param name="featList">所有菜单</param>
        /// <param name="tree">对应菜单树</param>
        /// <param name="ap">对应菜单选项卡</param>
        private void CreatMenu(string modalName, DataTable featList, Tree tree, AccordionPane ap, string iconUrl)
        {
            tree.Nodes.Clear();
            foreach (DataRow dr in featList.Select("modalName='" + modalName + "'"))
            {
                ExtAspNet.TreeNode node = new ExtAspNet.TreeNode();
                node.NodeID = dr["ID"].ToString();
                node.Text = dr["menuName"].ToString();
                node.IconUrl = iconUrl;
                node.NavigateUrl = dr["actWeb"].ToString();
                tree.Nodes.Add(node);
            }


            if (tree.Nodes.Count.Equals(0))
            {
                ap.Hidden = true;
                ap.Expanded = false;
                return;
            }
            ap.Hidden = false;
            ap.CssClass = String.Empty;
            //if (!alreadyShow)
            //{
            ap.CssClass = "tree_menu";
            ap.Expanded = true;
            //    alreadyShow = true;
            //}
        }

        protected void btn_EndLogin_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            LGSVR.BLL.Sys.UserBLL user = new LGSVR.BLL.Sys.UserBLL();
            user.logOut(Session["UserID"].ToString(), getIP());
            Response.Redirect("login.aspx");
        }
        //获取客户端IP地址 
        public string getIP()
        {
            string result = String.Empty;
            result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            if (null == result || result == String.Empty)
            {
                return "0.0.0.0";
            }
            return result;

        }
    }
}